﻿using Project_veilingsite.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Project_veilingsite.Controllers
{
    public class VeilingController : Controller
    {
        private dtb_Veilingssite _db = new dtb_Veilingssite();

        [HttpGet] //als de pag w opgehaald
        public ActionResult Index(string genres)
        {
            try
            {
                //een lijst maken die strings bevat
                var GenreList = new List<string>();
                //haal alle genres op
                var GenreQuery = from d in _db.Genres
                                 orderby d.genre
                                 select d.genre;
                //zorg dat er geen dubbele waarden zijn
                GenreList.AddRange(GenreQuery.Distinct());
                //dit heeft de dropdown list nodig 
                ViewBag.genres = new SelectList(GenreList);


                var model = TempData["SearchModel"];//het meegeven van het model (kom van de home controller)
                if (model == null) //als er geen model werd meegegeven haal dan alle veilingen op geordend op datum
                {
                    var model2 = _db.Veilingen.OrderByDescending(r => r.datum);

                    return View(model2);
                }
                else
                {
                    return View(model);
                }
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/LoggingTracing/tracingLogging.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException: " + e.StackTrace);
                }

                return View();
            }
        }

        [HttpPost]// als er iets nr de pag gestuurd wordt
        public ActionResult Index(String searchString, string genres, FormCollection prijs, String einddatum, String begindatum)
        {
            try
            {
                //moet altijd meegegeven w dus staat niet in de if strucuur
                var GenreList = new List<string>();
                var GenreQuery = from d in _db.Genres
                                 orderby d.genre
                                 select d.genre;

                GenreList.AddRange(GenreQuery.Distinct());
                ViewBag.genres = new SelectList(GenreList);

                if (!String.IsNullOrEmpty(searchString))
                {
                    var model =
                       _db.Veilingen
                       .OrderByDescending(r => r.datum)
                       .Where(r => searchString == null || r.titel.StartsWith(searchString))
                       ;

                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("_veilingen", model);
                    }


                    return View(model);
                }

                if (!string.IsNullOrEmpty(genres) && !genres.Equals("") && (Convert.ToInt32(prijs["Prijs"]) == 1))
                {

                    var number2 = (from d in _db.Genres
                                   orderby d.genre
                                   where d.genre.Equals(genres)
                                   select d.id).ToArray();
                    int genreIndex = number2[0];

                    var model =
                      _db.Veilingen
                      .OrderByDescending(r => r.datum)
                      .Where(r => r.genre == genreIndex && r.prijs >= 0 && r.prijs <= 50);
                    ;

                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("_veilingen", model);
                    }

                    return View(model);
                }
                if (!string.IsNullOrEmpty(genres) && !genres.Equals("") && (Convert.ToInt32(prijs["Prijs"]) == 2))
                {
                    var number2 = (from d in _db.Genres
                                   orderby d.genre
                                   where d.genre.Equals(genres)
                                   select d.id).ToArray();
                    int genreIndex = number2[0];

                    var model =
                      _db.Veilingen
                      .OrderByDescending(r => r.datum)
                      .Where(r => r.genre == genreIndex && r.prijs > 50 && r.prijs <= 100);
                    ;

                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("_veilingen", model);
                    }

                    return View(model);
                }
                if (!string.IsNullOrEmpty(genres) && !genres.Equals("") && (Convert.ToInt32(prijs["Prijs"]) == 3))
                {
                    var number2 = (from d in _db.Genres
                                   orderby d.genre
                                   where d.genre.Equals(genres)
                                   select d.id).ToArray();
                    int genreIndex = number2[0];

                    var model =
                      _db.Veilingen
                      .OrderByDescending(r => r.datum)
                      .Where(r => r.genre == genreIndex && r.prijs > 100 && r.prijs <= 200);
                    ;

                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("_veilingen", model);
                    }

                    return View(model);
                }
                if (!string.IsNullOrEmpty(genres) && !genres.Equals("") && (Convert.ToInt32(prijs["Prijs"]) == 4))
                {
                    var number2 = (from d in _db.Genres
                                   orderby d.genre
                                   where d.genre.Equals(genres)
                                   select d.id).ToArray();
                    int genreIndex = number2[0];

                    var model =
                      _db.Veilingen
                      .OrderByDescending(r => r.datum)
                      .Where(r => r.genre == genreIndex && r.prijs > 200);
                    ;

                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("_veilingen", model);
                    }

                    return View(model);
                }
                if (!string.IsNullOrEmpty(genres) && !genres.Equals(""))
                {
                    var number2 = (from d in _db.Genres
                                   orderby d.genre
                                   where d.genre.Equals(genres)
                                   select d.id).ToArray();
                    int genreIndex = number2[0];

                    var model =
                      _db.Veilingen
                      .OrderByDescending(r => r.datum)
                      .Where(r => r.genre == genreIndex);
                    ;

                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("_veilingen", model);
                    }

                    return View(model);
                }

                if (Convert.ToInt32(prijs["Prijs"]) == 1)
                {
                    var model =
                        _db.Veilingen
                        .OrderByDescending(r => r.datum)
                        .Where(r => r.prijs >= 0 && r.prijs <= 50);

                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("_veilingen", model);
                    }

                    return View(model);
                }

                if (Convert.ToInt32(prijs["Prijs"]) == 2)
                {
                    var model =
                        _db.Veilingen
                        .OrderByDescending(r => r.datum)
                        .Where(r => r.prijs > 50 && r.prijs <= 100);

                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("_veilingen", model);
                    }

                    return View(model);
                }
                if (Convert.ToInt32(prijs["Prijs"]) == 3)
                {
                    var model =
                         _db.Veilingen
                         .OrderByDescending(r => r.datum)
                         .Where(r => r.prijs > 100 && r.prijs <= 200);

                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("_veilingen", model);
                    }

                    return View(model);
                }
                if (Convert.ToInt32(prijs["Prijs"]) == 4)
                {
                    var model =
                         _db.Veilingen
                         .OrderByDescending(r => r.datum)
                         .Where(r => r.prijs > 200);

                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("_veilingen", model);
                    }

                    return View(model);
                }
                if (!begindatum.Equals(""))
                {
                    DateTime search_begindatum = Convert.ToDateTime(begindatum);
                    var model =
                        _db.Veilingen
                        .OrderByDescending(r => r.datum)
                        .Where(r => r.datum >= search_begindatum);

                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("_veilingen", model);
                    }

                    return View(model);
                }
                if (!einddatum.Equals(""))
                {
                    DateTime search_einddatum = Convert.ToDateTime(einddatum);
                    var model =
                        _db.Veilingen
                        .OrderByDescending(r => r.datum)
                        .Where(r => r.datum <= search_einddatum);
                   
                    if (Request.IsAjaxRequest())
                         {
                             return PartialView("_veilingen", model);
                         }

                    return View(model);

                }
                if (!begindatum.Equals("") && !einddatum.Equals(""))
                {
                    DateTime search_begindatum = Convert.ToDateTime(begindatum);
                    DateTime search_einddatum = Convert.ToDateTime(einddatum);

                    var model =
                        _db.Veilingen
                        .OrderByDescending(r => r.datum)
                        .Where(r => r.datum >= search_begindatum && r.datum <= search_einddatum);
                }
            }
            catch (Exception e)
            {
                //Server.MapPath("~") = de root-map van het project
                string pad = Server.MapPath("~") + "/LoggingTracing/tracingLogging.txt";
                if (!System.IO.File.Exists(pad))
                {
                    System.IO.File.Create(pad);
                }

                using (StreamWriter swTracing = System.IO.File.AppendText(pad))
                {
                    swTracing.Write("\n\nDatum/Tijd: " + DateTime.Now + "\nException: " + e.StackTrace);
                }
            }
            if (Request.IsAjaxRequest())
            {
                return PartialView("_veilingen");
            }

            return View();
        }
    }
}